package game.server;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;

public class SocketServer {
	
	public static void main(String[] args) {
		run();
	}

	private static void run() {
		int serverPort = 4700;
		ServerSocket serverSocket = null;
		int recvMsgSize = 0;
		byte[] receivBuf = new byte[32];
		
		try {
			serverSocket = new ServerSocket(serverPort);
			while(true) {
				System.out.println("服务端已启动，绑定端口" + serverPort);
				Socket clientSocket = serverSocket.accept();
				SocketAddress clientAddress = clientSocket.getRemoteSocketAddress();
				System.out.println("收到客户端连接，ip: ");
				
				InputStream in = clientSocket.getInputStream();
				OutputStream out = clientSocket.getOutputStream();
				
				while((recvMsgSize = in.read(receivBuf)) != -1) {
					String receiveData = new String(receivBuf.toString());
					System.out.println(receiveData);
					out.write(receivBuf,0,recvMsgSize);
				}
				
				// 释放Socket资源
				clientSocket.close();
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

}
